import json
import os

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

mystr=os.popen("tasklist")  #popen与system可以执行指令,popen可以接受返回对象
mystr=mystr.read() #读取输出
#print(mystr)
if mystr.find("firefox.exe") !=-1:
    os.popen("taskkill /IM firefox.exe")
# if mystr.find("browserOperation.exe")!=-1:
#     os.popen("taskkill /IM browserOperation.exe")
if mystr.find("geckodriver.exe")!=-1:
    os.popen("taskkill /IM geckodriver.exe")
load_dict = {}
with open("./url.json",'r') as load_f:
    load_dict = json.load(load_f)
browser = webdriver.Firefox()

wait = WebDriverWait(browser, 10)
arr = browser.window_handles
print(arr)
browser.get(load_dict['firstPageUrl'])
browser.maximize_window()
locator0 = (By.XPATH,load_dict['userNameInputPath'])
wait.until(EC.presence_of_element_located(locator0))
elem_user = browser.find_element_by_xpath(
    load_dict['userNameInputPath'])
elem_user.send_keys(load_dict["userName"])
elem_password = browser.find_element_by_xpath(
    load_dict['userPasswordPath'])
elem_password.send_keys(load_dict["userPassword"])
elem_password.send_keys(Keys.RETURN)
locator = (By.XPATH,load_dict["contentUrl"][0]["path"])
wait.until(EC.presence_of_element_located(locator))

def opeanMenu(list):
    for index in range(len(list)):
        row = list[index]
        elem_salesOutboundManagement = browser.find_element_by_xpath(
            row["path"])
        elem_salesOutboundManagement.click()
        if(index<len(list)-1):
            locator1 = (By.XPATH, list[index+1]["path"])
            wait.until(EC.presence_of_element_located(locator1))
try:
    opeanMenu(load_dict["contentUrl"])
except Exception as e:
    browser.close()
    print(e)


